﻿using System.IO;
using System.Net;
using System.Text;
using System;

namespace SmartCollector.Helper
{
    public class WebHelper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebHelper"/> class.
        /// </summary>
        private WebHelper()
        {
            
        }

        /// <summary>
        /// Get web page source from the URL
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static string GetPageSource1(string url)
        {
            WebResponse response = null;
            Stream stream = null;
            StreamReader
              reader = null;

            try
            {
                HttpWebRequest request =
                               (HttpWebRequest)WebRequest.Create(url);

                response = request.GetResponse();
                stream = response.GetResponseStream();

                if (!response.ContentType.ToLower().StartsWith("text/"))
                    return null;

                string buffer = "", line;

                reader = new StreamReader(stream,Encoding.Default);

                while ((line = reader.ReadLine()) != null)
                {
                    buffer += line + "\r\n";
                }

                return buffer;
            }
            catch (WebException e)
            {
                System.Console.WriteLine("Can't download:" + e);
                return null;
            }
            catch (IOException e)
            {
                System.Console.WriteLine("Can't download:" + e);
                return null;
            }
            finally
            {
                if (reader != null)
                    reader.Close();

                if (stream != null)
                    stream.Close();

                if (response != null)
                    response.Close();
            }
        }

        /// <summary>
        /// Gets the page source.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static string GetPageSource(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return string.Empty;
            }
            try
            {
                WebClient client = new WebClient();
                StringBuilder rs = new StringBuilder();
                client.Encoding = Encoding.UTF8;
                rs.Append(client.DownloadString(url));
                return rs.ToString();
            }
            catch (WebException web)
            {
                Console.WriteLine(web.Message);
            }
            return string.Empty;
        }
    }
}
